﻿#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    for (QThread* thread : threadVector) {
        thread->quit(); // 通知线程停止
        thread->wait(); // 等待线程结束
        delete thread;
    }

    connect(ui->toolButton_result, &QToolButton::clicked, this, &MainWindow::resultToolButtonClicked);
    connect(ui->pushButton_run, &QPushButton::clicked, this, &MainWindow::runButtonClicked);
    connect(ui->pushButton_addInput, &QPushButton::clicked, this, &MainWindow::addInputButtonClicked);
    connect(ui->pushButton_delInput, &QPushButton::clicked, this, &MainWindow::delInputButtonClicked);

    // 初始化添加1个输入文件的tab页
    SrcWidget* sw = new SrcWidget;
    ui->tabWidget->addTab(sw, u8"输入文件" + QString::number(ui->tabWidget->tabBar()->count() + 1));

    outFile = nullptr;

}

MainWindow::~MainWindow()
{
    delete ui;
}

QStringList MainWindow::searchInFile(QString filePath, QString objName)
{
    QStringList res;

    // 打开文件
    QXlsx::Document doc(filePath);

    // 获取文件有效列数
    int totalRow = doc.dimension().rowCount();
    int totalCol = doc.dimension().columnCount();

    // 获取查找标题内容
    for(int row = 1; row <= totalRow; ++row){
        for(int col = 1; col <= totalCol; ++col){
            resTitleList.append(doc.read(row, col).toString());
        }
    }

    return res;
}



void MainWindow::resultToolButtonClicked()
{
    QFileDialog dl;

    QString resultFilePath = "";

    resultFilePath = dl.getOpenFileName(nullptr, u8"选择输出文件", QApplication::applicationDirPath(), u8"excel文件 (*.xls *.xlsx)");

    ui->lineEdit_result->setText(resultFilePath);
}

void MainWindow::runButtonClicked()
{
    count = 0;

    if(!outFile) delete outFile;
    outFile = new QXlsx::Document;

    // 打开结果文件
    QXlsx::Document res(ui->lineEdit_result->text());

    srand(time(nullptr));
    for(int page = 0; page < ui->tabWidget->count(); ++page) {
        SrcWidget* sw = (SrcWidget*)ui->tabWidget->widget(page);

        // 更新数据对应关系
        sw->updateColumnMap();
        QString inputFilePath = sw->getFilePath();

        // 获取页面中的最新的数据
        QThread* thread = new QThread;
        threadVector.append(thread);
        ReadFile* rf = new ReadFile;
        rf->moveToThread(thread);
        connect(rf, &ReadFile::finishSignal, this, &MainWindow::finisedSlots);
        connect(thread, &QThread::finished, rf, [rf](){
            delete rf;
        });

        rf->readFile(outFile,
                     inputFilePath,
                     ui->lineEdit_result->text(),
                     ui->spinBox_startRow->value(),
                     ui->spinBox_endRow->value(),
                     sw->getDataMapVector());
        thread->start();
    }


}

void MainWindow::addInputButtonClicked()
{
    // 新增输入文件tab页
    SrcWidget* sw = new SrcWidget;
    int lastIndex = ui->tabWidget->addTab(sw, u8"输入文件" + QString::number(ui->tabWidget->count() + 1));

    // 切换到最新页
    ui->tabWidget->setCurrentIndex(lastIndex);
}

void MainWindow::delInputButtonClicked()
{
    if(ui->tabWidget->count() == 0) return;
    ui->tabWidget->removeTab(ui->tabWidget->count() - 1);
}



void MainWindow::finisedSlots()
{
    ++count;

    if(count == ui->tabWidget->count()){

        outFile->saveAs("./outfile.xlsx");


        QMessageBox::information(this, u8"提示", u8"数据提取完成！");

    }
}

